home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GearTest / GearBox.java.z / GearBox.java
Encoding:
Java Source  |  2003-08-08  |  11.5 KB  |  350 lines

  1. /*
  2.  *    @(#)GearBox.java 1.24 02/10/21 13:40:21
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.utils.behaviors.mouse.*;
  41. import java.applet.Applet;
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import com.sun.j3d.utils.applet.MainFrame;
  45. import com.sun.j3d.utils.universe.*;
  46. import javax.media.j3d.*;
  47. import javax.vecmath.*;
  48. import java.lang.Integer;
  49. import com.sun.j3d.utils.behaviors.vp.*;
  50.  
  51. public class GearBox extends Applet {
  52.  
  53.     static final int defaultToothCount = 48;
  54.     private int toothCount;
  55.     private SimpleUniverse u = null;
  56.     
  57.     public BranchGroup createGearBox(int toothCount) {
  58.     Transform3D tempTransform = new Transform3D();
  59.  
  60.     // Create the root of the branch graph
  61.     BranchGroup branchRoot = createBranchEnvironment();
  62.  
  63.         // Create a Transformgroup to scale all objects so they
  64.         // appear in the scene.
  65.         TransformGroup objScale = new TransformGroup();
  66.         Transform3D t3d = new Transform3D();
  67.         t3d.setScale(0.4);
  68.         objScale.setTransform(t3d);
  69.         branchRoot.addChild(objScale);
  70.  
  71.     // Create an Appearance.
  72.     Appearance look = new Appearance();
  73.     Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
  74.     Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
  75.     Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  76.     look.setMaterial(new Material(objColor, black,
  77.                       objColor, white, 100.0f));
  78.  
  79.  
  80.     // Create the transform group node and initialize it to the
  81.     // identity.  Enable the TRANSFORM_WRITE capability so that
  82.     // our behavior code can modify it at runtime.  Add it to the
  83.     // root of the subgraph.
  84.     TransformGroup gearboxTrans = new TransformGroup();
  85.     gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  86.     gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  87.     objScale.addChild(gearboxTrans);
  88.  
  89.     // Create a bounds for the mouse behavior methods
  90.         BoundingSphere bounds =
  91.             new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  92.     
  93.     // Define the shaft base information
  94.     int shaftCount = 4;
  95.     int secondsPerRevolution = 8000;
  96.  
  97.     // Create the Shaft(s)
  98.     Shaft shafts[] = new Shaft[shaftCount];
  99.     TransformGroup shaftTGs[] = new TransformGroup[shaftCount];
  100.     Alpha shaftAlphas[] = new Alpha[shaftCount];
  101.     RotationInterpolator shaftRotors[]
  102.         = new RotationInterpolator[shaftCount];
  103.     Transform3D shaftAxis[] = new Transform3D[shaftCount];
  104.  
  105.     // Note: the following arrays we're incorporated to make changing
  106.     // the gearbox easier.
  107.     float shaftRatios[] = new float[shaftCount];
  108.     shaftRatios[0] = 1.0f;
  109.     shaftRatios[1] = 0.5f;
  110.     shaftRatios[2] = 0.75f;
  111.     shaftRatios[3] = 5.0f;
  112.  
  113.     float shaftRadius[] = new float[shaftCount];
  114.     shaftRadius[0] = 0.2f;
  115.     shaftRadius[1] = 0.2f;
  116.     shaftRadius[2] = 0.2f;
  117.     shaftRadius[3] = 0.2f;
  118.  
  119.     float shaftLength[] = new float[shaftCount];
  120.     shaftLength[0] = 1.8f;
  121.     shaftLength[1] = 0.8f;
  122.     shaftLength[2] = 0.8f;
  123.     shaftLength[3] = 0.8f;
  124.  
  125.     float shaftDirection[] = new float[shaftCount];
  126.     shaftDirection[0] = 1.0f;
  127.     shaftDirection[1] = -1.0f;
  128.     shaftDirection[2] = 1.0f;
  129.     shaftDirection[3] = -1.0f;
  130.  
  131.     Vector3d shaftPlacement[] = new Vector3d[shaftCount];
  132.     shaftPlacement[0] = new Vector3d(-0.75, -0.9, 0.0);
  133.     shaftPlacement[1] = new Vector3d(0.75, -0.9, 0.0);
  134.     shaftPlacement[2] = new Vector3d(0.75, 0.35, 0.0);
  135.     shaftPlacement[3] = new Vector3d(-0.75, 0.60, -0.7);
  136.  
  137.     // Create the shafts.
  138.     for(int i = 0; i < shaftCount; i++) {
  139.         shafts[i] = new Shaft(shaftRadius[i], shaftLength[i], 25, look);
  140.     }
  141.  
  142.     // Create a transform group node for placing each shaft
  143.     for(int i = 0; i < shaftCount; i++) {
  144.         shaftTGs[i] = new TransformGroup();
  145.         gearboxTrans.addChild(shaftTGs[i]);
  146.         shaftTGs[i].getTransform(tempTransform);
  147.         tempTransform.setTranslation(shaftPlacement[i]);
  148.         shaftTGs[i].setTransform(tempTransform);
  149.         shaftTGs[i].addChild(shafts[i]);
  150.     }
  151.  
  152.     // Add rotation interpolators to rotate the shaft in the appropriate
  153.     // direction and at the appropriate rate
  154.     for(int i = 0; i < shaftCount; i++) {
  155.         shaftAlphas[i] = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
  156.                       (long)(secondsPerRevolution
  157.                          * shaftRatios[i]),
  158.                       0, 0,
  159.                       0, 0, 0);
  160.         shaftAxis[i] = new Transform3D();
  161.         shaftAxis[i].rotX(Math.PI/2.0);
  162.         shaftRotors[i]
  163.         = new RotationInterpolator(shaftAlphas[i], shafts[i],
  164.                        shaftAxis[i],
  165.                        0.0f,
  166.                        shaftDirection[i] * 
  167.                        (float) Math.PI * 2.0f);
  168.         shaftRotors[i].setSchedulingBounds(bounds);
  169.         shaftTGs[i].addChild(shaftRotors[i]);
  170.     }
  171.  
  172.     // Define the gear base information.  Again, these arrays exist to
  173.     // make the process of changing the GearBox via an editor faster
  174.     int gearCount = 5;
  175.     float valleyToCircularPitchRatio = .15f;
  176.     float pitchCircleRadius = 1.0f;
  177.     float addendum = 0.05f;
  178.     float dedendum = 0.05f;
  179.     float gearThickness = 0.3f;
  180.     float toothTipThickness = 0.27f;
  181.  
  182.     // Create an array of gears and their associated information
  183.     SpurGear gears[] = new SpurGear[gearCount];
  184.     TransformGroup gearTGs[] = new TransformGroup[gearCount];
  185.  
  186.     int gearShaft[] = new int[gearCount];
  187.     gearShaft[0] = 0;
  188.     gearShaft[1] = 1;
  189.     gearShaft[2] = 2;
  190.     gearShaft[3] = 0;
  191.     gearShaft[4] = 3;
  192.  
  193.     float ratio[] = new float[gearCount];
  194.     ratio[0] = 1.0f;
  195.     ratio[1] = 0.5f;
  196.     ratio[2] = 0.75f;
  197.     ratio[3] = 0.25f;
  198.     ratio[4] = 1.25f;
  199.  
  200.     Vector3d placement[] = new Vector3d[gearCount];
  201.     placement[0] = new Vector3d(0.0, 0.0, 0.0);
  202.     placement[1] = new Vector3d(0.0, 0.0, 0.0);
  203.     placement[2] = new Vector3d(0.0, 0.0, 0.0);
  204.     placement[3] = new Vector3d(0.0, 0.0, -0.7);
  205.     placement[4] = new Vector3d(0.0, 0.0, 0.0);
  206.  
  207.     // Create the gears.
  208.     for(int i = 0; i < gearCount; i++) {
  209.         gears[i]
  210.         = new SpurGearThinBody(((int)((float)toothCount * ratio[i])),
  211.                        pitchCircleRadius * ratio[i],
  212.                        shaftRadius[0],
  213.                        addendum, dedendum,
  214.                        gearThickness,
  215.                        toothTipThickness,
  216.                        valleyToCircularPitchRatio, look);
  217.     }
  218.  
  219.     // Create a transform group node for arranging the gears on a shaft
  220.     // and attach the gear to its associated shaft
  221.     for(int i = 0; i < gearCount; i++) {
  222.         gearTGs[i] = new TransformGroup();
  223.         gearTGs[i].getTransform(tempTransform);
  224.         tempTransform.rotZ((shaftDirection[gearShaft[i]] == -1.0) ?
  225.                    gears[i].getCircularPitchAngle()/-2.0f :
  226.                    0.0f);
  227.         tempTransform.setTranslation(placement[i]);
  228.         gearTGs[i].setTransform(tempTransform);
  229.         gearTGs[i].addChild(gears[i]);
  230.         shafts[gearShaft[i]].addChild(gearTGs[i]);
  231.     }
  232.  
  233.         // Have Java 3D perform optimizations on this scene graph.
  234.     branchRoot.compile();
  235.  
  236.     return branchRoot;
  237.     }
  238.  
  239.     BranchGroup createBranchEnvironment(){
  240.         // Create the root of the branch graph
  241.         BranchGroup branchRoot = new BranchGroup();
  242.          
  243.         // Create a bounds for the background and lights
  244.         BoundingSphere bounds =
  245.             new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  246.  
  247.         // Set up the background
  248.         Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
  249.         Background bgNode = new Background(bgColor);
  250.         bgNode.setApplicationBounds(bounds);
  251.         branchRoot.addChild(bgNode);
  252.  
  253.         // Set up the ambient light
  254.         Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
  255.         AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  256.         ambientLightNode.setInfluencingBounds(bounds);
  257.         branchRoot.addChild(ambientLightNode);
  258.  
  259.         // Set up the directional lights
  260.         Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
  261.         Vector3f light1Direction  = new Vector3f(1.0f, 1.0f, 1.0f);
  262.         Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
  263.     Vector3f light2Direction  = new Vector3f(-1.0f, -1.0f, -1.0f);
  264.  
  265.         DirectionalLight light1
  266.             = new DirectionalLight(light1Color, light1Direction);
  267.         light1.setInfluencingBounds(bounds);
  268.         branchRoot.addChild(light1);
  269.  
  270.         DirectionalLight light2
  271.             = new DirectionalLight(light2Color, light2Direction);
  272.         light2.setInfluencingBounds(bounds);
  273.         branchRoot.addChild(light2);
  274.  
  275.         return branchRoot;
  276.     }
  277.  
  278.     public GearBox() {
  279.     this(defaultToothCount);
  280.     }
  281.  
  282.     public GearBox(int toothCount) {
  283.     this.toothCount = toothCount;
  284.     }
  285.  
  286.     public void init() {
  287.     setLayout(new BorderLayout());
  288.         GraphicsConfiguration config =
  289.            SimpleUniverse.getPreferredConfiguration();
  290.  
  291.         Canvas3D c = new Canvas3D(config);
  292.     add("Center", c);
  293.     
  294.     // Create the gearbox and attach it to the virtual universe
  295.     BranchGroup scene = createGearBox(toothCount);
  296.     u = new SimpleUniverse(c);
  297.  
  298.     // add mouse behaviors to the ViewingPlatform
  299.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  300.  
  301.         // This will move the ViewPlatform back a bit so the
  302.         // objects in the scene can be viewed.
  303.     viewingPlatform.setNominalViewingTransform();
  304.  
  305.     // add orbit behavior to the ViewingPlatform
  306.     OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
  307.     BoundingSphere bounds =
  308.         new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  309.     orbit.setSchedulingBounds(bounds);
  310.     viewingPlatform.setViewPlatformBehavior(orbit);
  311.  
  312.     u.addBranchGraph(scene);
  313.     }
  314.  
  315.     public void destroy() {
  316.     u.cleanup();
  317.     }
  318.  
  319.     //
  320.     // The following allows GearBox to be run as an application
  321.     // as well as an applet
  322.     //
  323.     public static void main(String[] args) {
  324.     int value;
  325.     
  326.     if (args.length > 1) {
  327.         System.out.println("Usage: java GearBox  #teeth (LCD 4)");
  328.         System.exit(0);
  329.     } else if (args.length == 0) {    
  330.         new MainFrame(new GearBox(), 700, 700);
  331.     } else
  332.         {
  333.         try{
  334.             value = Integer.parseInt(args[0]);
  335.         } catch (NumberFormatException e) {
  336.             System.out.println("Illegal integer specified");
  337.             System.out.println("Usage: java GearBox  #teeth (LCD 4)");
  338.             value = 0;
  339.             System.exit(0);
  340.         }
  341.         if (value <= 0 | (value % 4) != 0) {
  342.             System.out.println("Integer not a positive multiple of 4");
  343.             System.out.println("Usage: java GearBox  #teeth (LCD 4)");
  344.             System.exit(0);
  345.         }
  346.         new MainFrame(new GearBox(value), 700, 700);
  347.         }
  348.     }
  349. }
  350.